package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 新增员工
     *
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public Result addEmp(EmployeeDTO dto) {
        log.info("进入了新增员工");
        //非空判断
        if(dto == null ){
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //查询数据库中是否存在账号，如果存在则返回错误
        Employee employee = employeeMapper.getByUsername(dto.getUsername());
        log.info("进入了判断调用");
        if (employee!=null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        //employee是查询过后的参数，他是不能被再次运用到查询，需要创建对象进行实例化
        employee = new Employee();
        //将dto内的数据存放进employee内
        BeanUtils.copyProperties(dto,employee);
        //调用新对象，运用mapper接口
        //密码加密
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        //状态
        employee.setStatus(StatusConstant.ENABLE);
        //新建时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        log.info("进入了sql");
        employeeMapper.addEmp(employee);
        //返回值
        return Result.success();
    }


    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login( EmployeeLoginDTO employeeLoginDTO) {
//        通过get方法调用当前参数列表当中的账号密码并存入username和password里
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /**
     * 员工分页查询
     *
     * @param employeePageQueryDTO
     * @return
     */
    @Override
    public Result page(EmployeePageQueryDTO employeePageQueryDTO) {
        //非空判断
        if (employeePageQueryDTO.getPage()==null){
          employeePageQueryDTO.setPage(1);
        }
        if (employeePageQueryDTO.getPageSize()==null){
            employeePageQueryDTO.setPageSize(10);
        }
        //开启分页
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        //调用mapper层，分页查询员工

        Page<Employee> employees = employeeMapper.PageQ(employeePageQueryDTO);

        //封装数据
        PageResult pageResult = new PageResult();
        pageResult.setTotal(employees.getTotal());
        pageResult.setRecords(employees);

        return Result.success(pageResult);
    }


}
